home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / misc1 / iv26_w30.zip / SOURCES / PAINT.C < prev    next >
C/C++ Source or Header  |  1992-03-25  |  5KB  |  236 lines

  1. /*
  2.  * Copyright (c) 1987, 1988, 1989 Stanford University
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and its
  5.  * documentation for any purpose is hereby granted without fee, provided
  6.  * that the above copyright notice appear in all copies and that both that
  7.  * copyright notice and this permission notice appear in supporting
  8.  * documentation, and that the name of Stanford not be used in advertising or
  9.  * publicity pertaining to distribution of the software without specific,
  10.  * written prior permission.  Stanford makes no representations about
  11.  * the suitability of this software for any purpose.  It is provided "as is"
  12.  * without express or implied warranty.
  13.  *
  14.  * STANFORD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  15.  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
  16.  * IN NO EVENT SHALL STANFORD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  17.  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  18.  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  19.  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  20.  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  21.  */
  22.  
  23. /*
  24.  * Graphics implementation on top of X
  25.  */
  26.  
  27. #include <InterViews/ftable.h>
  28. #include <InterViews/X11/palette.h>
  29. #include <InterViews/paint.h>
  30. #include <InterViews/strtable.h>
  31. #include <string.h>
  32.  
  33. Painter* stdpaint;
  34.  
  35. #ifdef _3D
  36. class Painter3D;
  37.  
  38. Painter3D* stdpaint3D;
  39. #endif
  40.  
  41. extern void InitColors();
  42.  
  43. static void MakeSubString (char* tmp, const char* s, int n) {
  44.     strncpy(tmp, s, n);
  45.     tmp[n] = '\0';
  46. }
  47.  
  48. /*
  49.  * class Brush
  50.  */
  51.  
  52. Brush* single;
  53.  
  54. Brush::Brush (int pat, int w) {
  55.     int r[16];
  56.     int count;
  57.     width = w;
  58.     unsigned int p = pat & 0xffff;
  59.     
  60.     if (p == 0xffff || p == 0) {
  61.     count = 0;
  62.     } else if (p == 0x5555 || p == 0xaaaa) {
  63.     r[0] = 1;
  64.     r[1] = 1;
  65.     count = 2;
  66.     } else if (p == 0xcccc || p == 0x3333) {
  67.     r[0] = 2;
  68.     r[1] = 2;
  69.     count = 2;
  70.     } else if (p == 0xf0f0 || p == 0x0f0f) {
  71.     r[0] = 4;
  72.     r[1] = 4;
  73.     count = 2;
  74.     } else if (p == 0xf000 || p == 0x000f) {
  75.     r[0] = 4;
  76.     r[1] = 12;
  77.     count = 2;
  78.     } else if (p == 0xff00 || p == 0x00ff) {
  79.     r[0] = 8;
  80.     r[1] = 8;
  81.     count = 2;
  82.     } else if (p == 0xfff0 || p == 0x0fff) {
  83.     r[0] = 12;
  84.     r[1] = 4;
  85.     count = 2;
  86.     } else {
  87.     unsigned int m = 1<<15;
  88.     int index = 0;
  89.     while ((p & m) == 0) {
  90.         m = m>>1;
  91.     }
  92.     while (m != 0) {
  93.         int length;
  94.         length = 0;
  95.         while(m != 0 && (p & m) != 0) {
  96.         ++length;
  97.         m = m>>1;
  98.         }
  99.         if (length > 0) {
  100.         r[index] = length;
  101.         ++index;
  102.         }
  103.         length = 0;
  104.         while (m != 0 && (p & m) == 0) {
  105.         ++length;
  106.         m = m>>1;
  107.         }
  108.         if (length > 0) {
  109.         r[index] = length;
  110.         ++index;
  111.         }
  112.     }
  113.     count = index - 1;
  114.     }
  115.     rep = new BrushRep(r, count, width);
  116. }
  117.  
  118. Brush::Brush (int* p, int c, int w) {
  119.     width = w;
  120.     rep = new BrushRep(p, c, w);
  121. }
  122.  
  123. Brush::~Brush () {
  124.     delete rep;
  125. }
  126.  
  127. /*
  128.  * class Color
  129.  */
  130.  
  131. Color* black;
  132. Color* white;
  133.  
  134. void InitColors() {
  135.     if (_palette == nil) {
  136.     _palette = new Palette();
  137.     }
  138.     black = new Color (0, 0, 0);
  139.     white = new Color (65535, 65535, 65535);
  140.     black->Reference();
  141.     white->Reference();
  142. }
  143.  
  144. Color::Color (ColorIntensity r, ColorIntensity g, ColorIntensity b) {
  145.     red = r;
  146.     green = g;
  147.     blue = b;
  148.     rep = new ColorRep(r, g, b);
  149. }
  150.  
  151. Color::Color (const char* name) {
  152.     rep = new ColorRep(name, red, green, blue);
  153. }
  154.  
  155. Color::Color (const char* name, int len) {
  156.     char tmp[100];
  157.  
  158.     MakeSubString(tmp, name, len);
  159.     rep = new ColorRep(tmp, red, green, blue);
  160. }
  161.  
  162. Color::Color (long pixel) {
  163.     rep = new ColorRep(pixel, red, green, blue);
  164. }
  165.  
  166. boolean Color::Valid () {
  167.     return rep != nil;
  168. }
  169.  
  170. Color::~Color () {
  171.     delete rep;
  172. }
  173.  
  174. /*
  175.  * class Font
  176.  */
  177.  
  178. Font* stdfont;
  179.  
  180. static FontTable* fontTable;
  181. static StringTable* fontnameTable;
  182.  
  183. Font::Font (const char* name) {
  184.     if (!Lookup(name, strlen(name))) {
  185.     GetFontByName(name);
  186.     }
  187. }
  188.  
  189. Font::Font (const char* name, int len) {
  190.     if (!Lookup(name, len)) {
  191.     if (name[len] == '\0') {
  192.         GetFontByName(name);
  193.     } else {
  194.         char tmp[256];
  195.  
  196.         MakeSubString(tmp, name, len);
  197.         GetFontByName(tmp);
  198.     }
  199.     }
  200. }
  201.  
  202. boolean Font::Lookup (const char* name, int len) {
  203.     StringId* s;
  204.  
  205.     if (fontTable == nil) {
  206.     fontTable = new FontTable(32);
  207.     fontnameTable = new StringTable(32);
  208.     }
  209.     s = fontnameTable->Id(name, len);
  210.     if (fontTable->Find(rep, s)) {
  211.     rep->Reference();
  212.     return true;
  213.     }
  214.     rep = new FontRep;
  215.     fontTable->Insert(s, rep);
  216.     return false;
  217. }
  218.  
  219. int Font::Index (const char* s, int offset, boolean between) {
  220.     return Index(s, strlen(s), offset, between);
  221. }
  222.  
  223. boolean Font::Valid() {
  224.     return (rep->id != nil);
  225. }
  226.  
  227. /*
  228.  * class Pattern
  229.  */
  230.  
  231. Pattern* solid;
  232. Pattern* clear;
  233. Pattern* lightgray;
  234. Pattern* gray;
  235. Pattern* darkgray;
  236.